package com.qujia.station.controller;

import com.qujia.common.annotation.Log;
import com.qujia.common.core.controller.BaseController;
import com.qujia.common.core.domain.AjaxResult;
import com.qujia.common.core.page.TableDataInfo;
import com.qujia.common.enums.BusinessType;
import com.qujia.station.domain.form.GoodsForm;
import com.qujia.station.domain.form.GoodsQueryForm;
import com.qujia.station.domain.vo.GoodsQueryVo;
import com.qujia.station.service.GoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Api(tags = "商品（非油品）管理")
@RestController
@RequestMapping("/goods")
public class GoodsController extends BaseController {

    @Autowired
    private GoodsService goodsService;

    @Log(title = "新增商品（非油品）", businessType = BusinessType.INSERT)
    @ApiOperation(value = "新增商品（非油品）", notes = "新增商品（非油品）")
    @PostMapping
    public AjaxResult add(@Valid @RequestBody GoodsForm goodsForm) {
        return toAjax(goodsService.insertGoods(goodsForm));
    }

    @Log(title = "修改商品信息（非油品）", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改商品信息（非油品）", notes = "修改商品信息（非油品）")
    @ApiImplicitParam(paramType = "path", name = "id", value = "商品ID", required = true)
    @PutMapping("/{id}")
    public AjaxResult update(@PathVariable Long id, @RequestBody GoodsForm goodsForm) {
        return toAjax(goodsService.updateGoods(id, goodsForm));
    }

    @ApiOperation(value = "获取商品信息（非油品）", notes = "获取商品信息（非油品）")
    @ApiImplicitParam(paramType = "path", name = "id", value = "商品ID", required = true)
    @GetMapping(value = "/{id}")
    public AjaxResult get(@PathVariable Long id) {
        return AjaxResult.success(goodsService.selectGoodsById(id));
    }

    @Log(title = "逻辑删除商品信息（非油品）", businessType = BusinessType.DELETE)
    @ApiOperation(value = "逻辑删除商品信息（非油品）", notes = "逻辑删除商品信息（非油品）")
    @ApiImplicitParam(paramType = "path", name = "ids", value = "商品ID集合，多个用','分隔", required = true)
    @DeleteMapping("/{ids}")
    public AjaxResult delete(@PathVariable List<Long> ids) {
        goodsService.deleteGoods(ids);
        return AjaxResult.success();
    }

    @GetMapping("/page")
    @ApiOperation(value = "商品信息（非油品）分页", notes = "商品信息（非油品）分页")
    public TableDataInfo page(GoodsQueryForm queryForm) {
        startPage();
        List<GoodsQueryVo> goodsQueryVoList = goodsService.pageGoods(queryForm);
        return getDataTable(goodsQueryVoList);
    }

    @Log(title = "修改商品状态信息（非油品）", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改商品状态信息（非油品）", notes = "修改商品状态信息（非油品）")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "id", value = "商品ID", required = true),
            @ApiImplicitParam(paramType = "path", name = "status", value = "状态(1:待上架 2:已下架 3:在售 4:已售空)", required = true),
            })
    @PutMapping("/id/{id}/status/{status}")
    public AjaxResult updateStatus(@PathVariable("id") Long id, @PathVariable("status") Integer status) {
        return toAjax(goodsService.updateStatus(id, status));
    }
}
